#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-08-20 6:35 下午
# @Author  : yangdy
# @File    : 2020-08-20.529.扫雷游戏
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        # ans = []
        # ans = board.copy()
        if not board:
            return board
        n = len(board)
        m = len(board[0])
        sp = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]

        def dfs(x, y):
            if board[x][y] == 'M':
                board[x][y] = 'X'
                return
            ms = 0
            es = []
            for spi in sp:
                xx, yy = x+spi[0], y+spi[1]
                if 0 <= xx < n and 0 <= yy < m:
                    if board[xx][yy] == 'M':
                        ms += 1
                    elif board[xx][yy] == 'E':
                        es.append((xx, yy))
            if ms != 0:
                board[x][y] = chr(ord('0')+ms)
                return
            board[x][y] = 'B'
            # for spi in sp:
            #     xx, yy = x + spi[0], y + spi[1]
            #     if 0 <= xx < n and 0 <= yy < m:
            #         if board[xx][yy] == 'E':
            #             dfs(xx, yy)
            for ei in es:
                dfs(ei[0], ei[1])
        dfs(click[0], click[1])

        return board


questions = [
    ([['E', 'E', 'E', 'E', 'E'],
      ['E', 'E', 'M', 'E', 'E'],
      ['E', 'E', 'E', 'E', 'E'],
      ['E', 'E', 'E', 'E', 'E']],
     [3, 0]
     ),
]

work(questions, lambda q:Solution().updateBoard(q[0], q[1]))
